package com.java.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MenuTest implements ActionListener{
	private JMenuBar bar;
	private JFrame frame;
	private JMenu file,edit,view;
	private JMenuItem[][] items;
	public MenuTest(){
		this.frame=new JFrame("菜单测试");
		this.bar=new JMenuBar();
		file=new JMenu("文件");
		edit=new JMenu("编辑");
		view=new JMenu("视图");
		items=new JMenuItem[3][];
		addItem();
	}
	public void addItem(){
		items[0]=new JMenuItem[5];
		file.add(items[0][0]=new JMenuItem("新建"));
		file.add(items[0][1]=new JMenuItem("打开.."));
		file.add(items[0][2]=new JMenuItem("保存"));
		file.addSeparator();//添加分隔线
		file.add(items[0][3]=new JMenuItem("另存为.."));
		file.add(items[0][4]=new JMenuItem("退出"));
		
		items[1]=new JMenuItem[3];
		edit.add(items[1][0]=new JMenuItem("查找"));
		edit.add(items[1][1]=new JMenuItem("复制"));
		edit.add(items[1][2]=new JMenuItem("粘贴"));
		
		items[2]=new JMenuItem[2];
		view.add(items[2][0]=new JMenuItem("工具栏"));
		view.add(items[2][1]=new JMenuItem("颜色.."));
		
		
		bar.add(file);
		bar.add(edit);
		bar.add(view);
		
//		frame.add(bar,BorderLayout.NORTH);
		frame. setJMenuBar(bar);
		addEventHandler();
	}
	public void showMe(){
		frame.setSize(400, 300);
	    frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}

//	public void addEventHandler(){
//		items[0][1].addActionListener(new ActionListener(){
//
//			public void actionPerformed(ActionEvent e) {
//				JFileChooser jfc=new JFileChooser();
//				jfc.showOpenDialog(frame);
////				File f=jfc.getSelectedFile();
//			}
//			
//		});
//		items[2][1].addActionListener(new ActionListener(){
//			public void actionPerformed(ActionEvent e) {
//				Color c=JColorChooser.showDialog(frame, "请选择颜色", Color.BLUE);
//				frame.getContentPane().setBackground(c);
//			}
//		});
//	}
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("打开..")){
			JFileChooser jfc=new JFileChooser();
			jfc.showOpenDialog(frame);
			File f=jfc.getSelectedFile();
		}else{
			Color c=JColorChooser.showDialog(frame, "请选择颜色", Color.BLUE);
			frame.getContentPane().setBackground(c);
		}
	}
	public void addEventHandler(){
		items[0][1].addActionListener(this);
		items[2][1].addActionListener(this);
	}
	public static void main(String[] args){
		new MenuTest().showMe();
	}
}
